home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / faq / comp / forthfaq / whatisfo < prev   
Internet Message Format  |  1993-12-14  |  10KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!nic.hookup.net!news.moneng.mei.com!howland.reston.ans.net!gatech!pitt!willett!ForthFAQ
  2. From: ForthFAQ@willett.pgh.pa.us (FAQ account for comp.lang.forth)
  3. Newsgroups: comp.lang.forth,comp.answers,news.answers
  4. Subject: Forth FAQ: What is Forth?  (l/m 30.Jan.93)
  5. Message-ID: <4820.UUL1.3#5129@willett.pgh.pa.us>
  6. Date: 15 Dec 93 01:39:20 GMT
  7. Expires: Wed, 22 Dec 93 23:59:59 EDT
  8. References: <4819.UUL1.3#5129@willett.pgh.pa.us>
  9. Followup-To: poster
  10. Lines: 226
  11. Approved: news-answers-request@MIT.Edu
  12. Xref: senator-bedfellow.mit.edu comp.lang.forth:14685 comp.answers:3016 news.answers:15813
  13.  
  14. Archive-name: ForthFaq/WhatIsForth
  15. Last-modified: 30.Jan.93
  16. Version: 1.2
  17.  
  18.  
  19.  
  20. What is Forth?
  21.  
  22.  
  23.  ------------------------------------------------------------
  24.  
  25.  Philip J. Koopman Jr.
  26.  United Technologies Research Center, East Hartford, CT
  27.  
  28.  This description is copyright 1993 by ACM, and was developed
  29.  for the Second History of Programming Languages Conference
  30.  (HOPL-II), Boston MA.
  31.  
  32.  Permission to copy without fee all or part of this material
  33.  is granted, provided that the copies are not made or
  34.  distributed for direct commercial advantage, the ACM
  35.  copyright notice and the title of the publication and its
  36.  data appear, and notice is given that copying is by
  37.  permission of the Association for Computing Machinery.  To
  38.  copy otherwise, or to republish, requires a fee and/or
  39.  specific permission.
  40.  
  41.  ------------------------------------------------------------
  42.  
  43.  
  44.  
  45.  
  46.                  A BRIEF INTRODUCTION TO FORTH
  47.                  -----------------------------
  48.  
  49.       Forth is both an extensible language and an interactive
  50.  program development methodology.  Originally developed for
  51.  small embedded control mini- and micro-computers, Forth
  52.  seems to have been implemented on every major processor
  53.  manufactured.  It has been used in a wide variety of
  54.  applications, including spreadsheets, expert systems, and
  55.  multi-user databases.
  56.  
  57.  
  58.  TWO-STACK ABSTRACT MACHINE
  59.  
  60.       At the most superficial level, Forth is a directly
  61.  executable language for a stack-based abstract machine.  In
  62.  its essential form, the Forth abstract machine has a program
  63.  counter, memory, ALU, data evaluation pushdown stack, and
  64.  subroutine return address pushdown stack.
  65.  
  66.       Data evaluation in Forth is accomplished on the Data
  67.  Stack using Reverse Polish Notation (RPN), also called
  68.  postfix notation.  For example, the following sequence typed
  69.  from the keyboard:
  70.  
  71.       3 4 +  5 *  .  __35 ok__
  72.  
  73.  interactively pushes the value 3 on the stack, pushes the
  74.  value 4 on top of the 3, destructively adds 3 and 4 to get
  75.  7, then multiplies by 5.  The . operation displays the
  76.  single resultant top value on the stack, 35 (computer output
  77.  is underlined).  "ok" is the Forth command prompt.
  78.  Operations such as SWAP and DUP (duplicate) reorder and
  79.  replicate the top few Data Stack elements.
  80.  
  81.  
  82.  FACTORING
  83.  
  84.       At a deeper level, Forth programs use RPN not as an end
  85.  in itself, but rather as a means to achieve simple syntax
  86.  and flexible modularity.  Small, simple programs to perform
  87.  complex functions are written by reusing common code
  88.  sequences through a programming practice known as factoring.
  89.  
  90.       Subroutine calls and returns are an important part of
  91.  Forth programs and the factoring process.  As an example,
  92.  consider the following function (called a word in Forth)
  93.  which computes the sum of squares of two integers on top of
  94.  the Data Stack and returns the result on the Data Stack:
  95.  
  96.  : SUM-OF-SQUARES   ( a b -- c )   DUP *   SWAP   DUP *  +  ;
  97.  
  98.  The Data Stack inputs to the word at run-time are two
  99.  integers a and b.  The Data Stack output is a single integer
  100.  c.  The : denotes a function definition with the name SUM-
  101.  OF-SQUARES.  The ; terminates the definition.  Comments are
  102.  enclosed in parentheses.  This example follows the Forth
  103.  convention of including a stack-effect comment showing that
  104.  a (the second stack element) and b (the top stack element)
  105.  are consumed as stack inputs, with c produced as the stack
  106.  output.
  107.  
  108.       By the process of factoring, the example program would
  109.  be re-written in Forth using a new definition (a factor)
  110.  called SQUARED to allow sharing the common function of
  111.  duplicating and multiplying a number on the Data Stack.  The
  112.  separation of the Return Stack from the Data Stack in the
  113.  abstract machine allows the values on the Data Stack to be
  114.  cleanly passed down through multiple levels of subroutine
  115.  calls without run-time overhead.  In this new version, Data
  116.  Stack elements are implicitly passed as parameters from SUM-
  117.  OF-SQUARES to SQUARED:
  118.  
  119.  : SQUARED   ( n -- n**2 )     DUP *  ;
  120.  : SUM-OF-SQUARES   ( a b -- c )  SQUARED  SWAP SQUARED  +  ;
  121.  
  122.       Good Forth programmers strive to write programs
  123.  containing very short (often one-line), well-named word
  124.  definitions and reused factored code segments.  The ability
  125.  to pick just the right name for a word is a prized talent.
  126.  Factoring is so important that it is common for a Forth
  127.  program to have more subroutine calls than stack operations.
  128.  Factoring also simplifies speed optimization via replacing
  129.  commonly used factors with assembly language definitions.
  130.  In the preceding example, SQUARED could be re-written in
  131.  assembly language for speed while maintaining the same stack
  132.  effects.
  133.  
  134.       Writing a Forth program is equivalent to extending the
  135.  language to include all functions needed to implement an
  136.  application.  Therefore, programming in Forth may be thought
  137.  of as creating an application-specific language extension.
  138.  This paradigm, when coupled with a very quick
  139.  edit/compile/test cycle, seems to significantly increase
  140.  productivity.  As each Forth word is written, it can be
  141.  tested from the keyboard for immediate programmer feedback.
  142.  For example, the definitions above could be summarily tested
  143.  with:
  144.  
  145.  3 SQUARED .   __9 ok__
  146.  3 4 SUM-OF-SQUARES .   __25 ok__
  147.  
  148.  
  149.  INTERPRETATION, COMPILATION AND EXECUTION
  150.  
  151.       Forth systems use two levels of interpretation: a text
  152.  interpreter and an address interpreter.  When accepting
  153.  keyboard or file-based input, the text interpreter extracts
  154.  whitespace-separated character strings.  In interpretation
  155.  mode it attempts to execute the corresponding words (numeric
  156.  input is trapped and converted as a special case).  : is a
  157.  word like any other, but creates a new dictionary entry
  158.  containing the word name (symbol) and places the text
  159.  interpreter into compilation mode.  While in compilation
  160.  mode, most words extracted from the input stream are
  161.  compiled to a pointer to the word's definition in the
  162.  dictionary instead of being executed.
  163.  
  164.       A compiled Forth program is a collection of words, each
  165.  of which contains a statically allocated list of pointers to
  166.  other words.  Ultimately the pointers lead to assembly
  167.  language primitives, some of which are typically user-
  168.  written.  The Forth address interpreter is used to execute
  169.  compiled words, classically using threaded code techniques.
  170.  The Forth text interpreter, while not used in executing
  171.  compiled programs, is often included in applications as the
  172.  basis of a command-line user interface.
  173.  
  174.       Forth systems use one-pass compilation.  There is no
  175.  explicit Forth parser (and, for practical purposes, no
  176.  formal grammar).  Control flow words have a special
  177.  immediate attribute, and are executed immediately even when
  178.  the text interpreter is in compilation mode.  Immediate
  179.  words, when executed, typically cause compilation of special
  180.  structures.  For example, IF compiles a branch conditional
  181.  upon the top runtime Data Stack value, and the matching THEN
  182.  (the "endif" word) back-patches the branch target address.
  183.  Users can readily create their own immediate words, thus
  184.  extending the compiler by adding new control flow structures
  185.  or other language features.
  186.  
  187.       Data structures are created by another special class of
  188.  words: defining words.  Defining words have two parts: the
  189.  CREATE clause creates the dictionary entry for the data
  190.  structure instance, while the DOES> clause is a definition
  191.  shared by all data structures created by that defining word.
  192.  For example, an array defining word creates a named array
  193.  and reserves storage with its CREATE clause, and computes an
  194.  address (given indices) in its DOES> clause.  Defining words
  195.  are commonly used to hide data structure implementations and
  196.  to create families of similar words.
  197.  
  198.       Forth programmers traditionally value complete
  199.  understanding and control over the machine and their
  200.  programming environment.  Therefore, what Forth compilers
  201.  don't do reveals something about the language and its use.
  202.  Type checking, macro preprocessing, common subexpression
  203.  elimination, and other traditional compiler services are
  204.  feasible, but not included in production Forth compilers.
  205.  This simplicity allows Forth development systems to be small
  206.  enough to fit in the on-chip ROM of an 8-bit
  207.  microcontroller.  On the other hand, Forth's extensibility
  208.  allows "full-featured" systems to consume over 100K bytes
  209.  and provide comprehensive window-based programming
  210.  environments.  Forth also allows (and often encourages)
  211.  programmers to completely understand the entire compiler and
  212.  run-time system.  Forth supports extremely flexible and
  213.  productive application development while making ultimate
  214.  control of both the language and hardware easily attainable.
  215.  
  216.  
  217.  Philip J. Koopman Jr.
  218.  United Technologies Research Center, East Hartford, CT
  219.  
  220.  This description is copyright 1993 by ACM, and was developed
  221.  for the Second History of Programming Languages Conference
  222.  (HOPL-II), Boston MA.
  223.  
  224.  Permission to copy without fee all or part of this material
  225.  is granted, provided that the copies are not made or
  226.  distributed for direct commercial advantage, the ACM
  227.  copyright notice and the title of the publication and its
  228.  data appear, and notice is given that copying is by
  229.  permission of the Association for Computing Machinery.  To
  230.  copy otherwise, or to republish, requires a fee and/or
  231.  specific permission.
  232.  
  233. ---
  234. If you have any questions about ForthNet/comp.lang.forth or any information
  235. to add/delete or correct in this message or any suggestions on formatting or
  236. presentation, please contact Doug Philips at one of the following addresses:
  237.           Internet: dwp@willett.pgh.pa.us
  238.           Usenet:   ...!uunet!willett.pgh.pa.us!dwp
  239.           GEnie:    D.PHILIPS3
  240.